﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xyz.BusinessObjects.Validation;

namespace Xyz.BusinessObjects
{
    public abstract class BusinessObject
    {
        protected static readonly string VersionDefault = string.Empty;

        private List<ValidationRule> validationRules = new List<ValidationRule>();
        private List<string> validationErrors = new List<string>();

        public List<string> ValidationErrors
        {
            get { return validationErrors; }
        }

        protected void AddRule(ValidationRule validationRule)
        {
            validationRules.Add(validationRule);
        }

        public bool Validate()
        {
            bool isValid = true;

            validationErrors.Clear();

            foreach (ValidationRule rule in validationRules)
            {
                if (!rule.Validate(this))
                {
                    isValid = false;
                    validationErrors.Add(rule.ErrorMessage);
                }
            }

            return isValid;
        }
    }
}
